<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
</head>
<body>
<!--编辑弹出框-->
<div class="site-text" style="margin: 5%; display: none" id="edit_window">
    <div class="layui-form" style="width: 400px;" lay-filter="editForm">
        <!--隐藏域，id-->
        <input type="hidden" id="id" name="id" class="layui-input" value="null">
        <!--用户名-->
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" id="usernamea" name="studentnamea" value="" placeholder="请输入用户名" class="layui-input" required>
            </div>
        </div>
        <!--密码-->
        <div class="layui-form-item">
            <label class="layui-form-label">QQ</label>
            <div class="layui-input-block">
                <input type="text" id="userpassworda" name="studentpassworda" value="" placeholder="请输入QQ" class="layui-input" required>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input type="text" id="studentnumbera" name="studentnumbera" value="" placeholder="请输入编号" class="layui-input" required>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话号码</label>
            <div class="layui-input-block">
                <input type="text" id="studentphonea" name="studentphonea" value="" placeholder="请输入电话号码" class="layui-input" required>
            </div>
        </div>
    </div>
</div>
<!--数据表格布局-->
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--搜索栏-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="searchForm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="studentnamea" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜　　索 </button>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-all-btn"><i class="layui-icon"></i> 显示全部 </button>
                        </div>
                    </div>
                </form>
                <p>搜索框支持模糊查询。</p>
            </div>
        </fieldset>
        <!--头部工具栏-->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加用户 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除选中行 </button>
            </div>
        </script>
        <!--表格容器-->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <!--行工具栏-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<!--js代码-->
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery, form = layui.form, table = layui.table;
        //加载数据表格
        table.render({
            elem: '#currentTableId',
            url: '/student/selectAlla',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox"},
                {field: 'id', title: '序号', sort: true},
                {field: 'studentnamea', title: '用户名'},
                {field: 'studentpassworda', title: 'QQ'},
                {field: 'studentnumbera', title: '编号'},
                {field: 'studentphonea', title: '手机号码'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 15, 20, 25, 50, 100],
            limit: 10,
            page: {
                prev: '上一页',
                next: '下一页',
            },
            done: function (res) {
                console.log(res);
            },
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            console.log(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                url: '/student/selectByConditiona',
                where: {
                    studentnamea:data.field.studentnamea,
                },   //把json传过去
                page: {curr: 1}, //重新从第 1 页开始
                done: function (res) {
                    layer.msg("搜索到"+res.count+"个结果", {time:800});
                    return res;
                }
            });
            return false;   //不跳转
        });

        // 监听显示全部操作
        form.on('submit(data-all-btn)', function (data) {
            form.val("searchForm", {
                'username':null
            });
            //执行搜索重载
            table.reload('currentTableId', {
                url: '/student/selectAlla',
                page: {curr: 1}, //重新从第 1 页开始
                done: null
            });
            return false;   //不跳转
        });

        //toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                layer.open({
                    title:"新增",
                    type: 1,
                    maxmin:true,
                    shadeClose: true,
                    area:['500px','550px'],
                    btn: ['确定', '取消'],
                    content: $("#edit_window"),
                    success: function () {  //弹出框成功回调
                        //给表单赋值
                        form.val("editForm", {
                            "id": null,
                            "studentnamea": null,
                            "studentpassworda": null,
                            "studentnumbera": null,
                            "studentphonea": null
                        });
                    },
                    yes: function(index,layero){ //确认的回调
                        layer.close(index); //关闭弹出框
                        var mdata = form.val('editForm');
                        //向服务器请求
                        $.getJSON({
                            url: '/student/insertStudenta',
                            data: {json:JSON.stringify(mdata)},    //发json
                            success:function (res) {
                                layer.msg("添加"+res+"行成功！",{time:800});
                                //获得最后一页的页码并重载
                                location.reload();
                            }
                        });
                    }
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                layer.confirm('确定要删除选中行吗？', function(index){
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;
                    layer.close(index); //关闭提示框
                    //向服务器请求
                    $.getJSON({
                        url: '/student/deleteUsersa',
                        data: {json:JSON.stringify(data)},   //发json过去
                        success:function (res) {
                            switch (res) {
                                case 0:
                                    layer.msg("您有花未归还，不能删除！");
                                    break;
                                case 1:
                                    layer.msg("删除" + res + "行成功！", {time: 800});
                                    //重载表格
                                    table.reload('currentTableId',{page:{curr:1}});
                                    break;
                            }
                        }
                    });
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') { //监听编辑按钮
                var index = layer.open({
                    title: '编辑用户',
                    type: 1,    //界面层
                    maxmin:true,
                    shadeClose: true,
                    area: ['500px', '550px'],
                    btn: ['确定', '取消'],
                    content: $("#edit_window"),
                    success: function () {
                        var mdata = obj.data;   //获取该行的数据
                        //给表单赋值
                        form.val("editForm", {
                            "id": mdata.id,
                            "studentnamea": mdata.studentnamea,
                            "studentpassworda": mdata.studentpassworda,
                            "studentnumbera": mdata.studentnumbera,
                            "studentphonea": mdata.studentphonea,
                        });
                    },
                    yes: function () {  //确认回调
                        layer.close(index); //关闭弹出框
                        var mdata = form.val('editForm');   //获取表单的数据
                        $.getJSON({
                            url: '/student/updateByIda',
                            data: {json:JSON.stringify(mdata)},   //发json过去
                            success:function (res) {
                                layer.msg("修改"+res+"行成功!",{time:800});
                                //重载表格
                                table.reload('currentTableId');
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {    //监听删除按钮
                layer.confirm('确定要删除该行吗？', function (index) {
                    var mdata = obj.data;    //获取该行的数据
                    //向服务器请求
                    $.getJSON({
                        url: '/student/deleteStudenta',
                        data: {json:JSON.stringify(mdata)},   //发json过去
                        success:function (res) {
                            switch (res) {
                                case 0:
                                    layer.msg("有其他关系不能能删除！");
                                    break;
                                case 1:
                                    obj.del();  //删除对应行（tr）的DOM结构，并更新缓存
                                    layer.close(index); //关闭窗口
                                    layer.msg("删除" + res + "行成功！", {time: 800});
                                    break;
                            }
                        }
                    });
                });
            }
        });
        $.getJSON({
            url: '/faculty/selectAlla',
            success: function (data) {
                // $("#edit_booktypeid").html("");
                $.each(data,function (i,n) {
                    $("#studentfacultyid")
                        .append("<option value=\""+n.id+"\">"+n.facultyname+"</option>"),
                    $("#studentfacultyid1")
                        .append("<option value=\""+n.id+"\">"+n.facultyname+"</option>")
                });
                form.render('select','editForm'); //刷新select选择框渲染
                form.render('select','searchForm'); //刷新select选择框渲染

            }
        });

        $.getJSON({
            url: '/major/selectAlla',
            success: function (data) {
                // $("#edit_booktypeid").html("");
                $.each(data,function (i,n) {
                    $("#studentmajorid")
                        .append("<option value=\""+n.id+"\">"+n.majorname+"</option>"),
                    $("#studentmajorid1")
                        .append("<option value=\""+n.id+"\">"+n.majorname+"</option>")
                });
                form.render('select','editForm'); //刷新select选择框渲染
                form.render('select','searchForm'); //刷新select选择框渲染

            }
        });

    });
</script>

</body>
</html>